$(function () {

    $(".close").each(function () {
        $(this).click(function () {
            $(this).parent().css("visibility","hidden");
        })

    })

    $("#my-info").click(function () {
        $("#common_dialog").css("visibility","visible");
    })

    $("#modify-password").click(function () {
        $("#forbidDialog").css("visibility","visible");
    })

    $("#change-headimg").click(function () {
        $("#change-div").css("display","block");
    })

    $("#ok-change-info").click(function () {
        var nickname = $("#nickname").val();
        var email = $("#email").val();

        if (nickname.length < 6 || nickname > 16) {
            alert("账户为6-16字符");
            return false;
        }
        if (nickname == "") {
            alert("昵称不能为空");
            return false;
        }
        var grep =  /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
        if (!grep.test(email)) {
            $('#userCue').html("邮箱格式不正确");return false;
        }
        $.get("/user/change_info?nickname="+nickname+"&email="+email,function (result) {
            alert(result.msg);
        })

    })

    //修改头像
    $("#ok-change-headimg").click(function () {
        $.ajaxFileUpload({
            url:"/user/change_headimg",
            fileElementId:"file",
            dataType: 'json', //返回值类型 一般设置为json
            success: function (data){
                if(data.success){
                    location.reload();
                }else {
                    alert(data.msg);
                }
            }
        })

    })

    //修改密码
    $("#ok-password").click(function () {
        var oldpassword = $("#oldpassword").val();
        var newpassword = $("#newpassword").val();
        var conpassword = $("#con-password").val();
        if (oldpassword == ""){
            alert("旧密码不能为空");return;
        }
        if (newpassword == ""){
            alert("新密码不能为空");return;
        }
        if (newpassword.length<6 || newpassword.length>16){
            alert("密码的长度为6到16位");return;
        }
        if (newpassword != conpassword){
            alert("两次密码不相同");return;
        }

        $.post("/user/alter_user_password",{"oldPassword":oldpassword,"newPassword":newpassword},function (data) {
            alert(data.msg);
            if (data.success){
                $("#forbidDialog").css("visibility","hidden");
            }
        })
    })

})

